package com.ll.domain.classbase.event.consumer;

import com.ll.base.constant.LlConstants;
import com.ll.base.util.DateUtil;
import com.ll.base.util.RedisUtil;
import com.ll.domain.classbase.dto.CourseDTO;
import com.ll.domain.classbase.event.producer.UpdateCourserStatusEvent;
import com.ll.domain.dingtalk.base.DingTalkMessageBaseService;
import com.ll.domain.ranking.dto.RankingDTO;
import com.ll.domain.ranking.enums.RankingTypeEnum;
import com.ll.domain.ranking.producer.CleanRankingEvent;
import com.ll.domain.user.base.UserBaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * pk 记录添加
 * @author WuHen
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class UpdateCourserStatusEventToRobotListener implements ApplicationListener<UpdateCourserStatusEvent> {

    private final RedisUtil redisUtil;

    @Async
    @Override
    public void onApplicationEvent(UpdateCourserStatusEvent event) {
        CourseDTO courseDTO = event.getCourseDTO();
        log.info("接收到 课程信息修改状态事件 id ：{}", courseDTO.getId());
        //刷新机器人map 里面的课程状态
        Object mapObj = redisUtil.get(LlConstants.RedisContent.COURSE_LIST);
        Map<String, CourseDTO> map = new HashMap<>();
        if(mapObj != null){
            map.forEach((k,v)->{
                if(v != null && v.getId().equals(courseDTO.getId())){
                    v.setStatus(courseDTO.getStatus());
                }
            });
        }
        redisUtil.set(LlConstants.RedisContent.COURSE_LIST, map, DateUtil.getDistanceTimeHour());
        log.info("接收到 课程信息修改状态事件 id ：{} 处理成功", courseDTO.getId());
    }
}
